﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Modele;

namespace Guitar.Vues
{
    /// <summary>
    /// Logique d'interaction pour VueMesure.xaml
    /// </summary>
    public partial class VueMesure : Grid
    {

        public bool isSelected = false;

        private Boolean _IsClassicNotationEnabled = App.option.Affichage.IsNotationClassicEnabled;
        public Boolean IsClassicNotationEnabled
        {
            get { return _IsClassicNotationEnabled; }
            set
            {
                _IsClassicNotationEnabled = value;

                /* on gére l'affichage dynamiquement */
                if (_IsClassicNotationEnabled)
                {
                    NotationClassique.Visibility = Visibility.Visible;
                    DebutMesure.Height = 103;

                    DebutMesure.Visibility = Visibility.Visible;

                }
                else
                {
                    NotationClassique.Visibility = Visibility.Collapsed;
                    DebutMesure.Height = 36;

                }
            }
        }

        private int _numeroMesure = 0;
        public int numeroMesure
        {
            get { return _numeroMesure; }
            set { _numeroMesure = value; lblNumMesure.Content = _numeroMesure; }
        }
        private bool _isFirstMesure = false;
        public bool isFirstMesure
        {
            get { return _isFirstMesure; }
            set { _isFirstMesure = value; firstMesure(_isFirstMesure); }
        }

        public static readonly DependencyProperty isFirstMesureProperty =
           DependencyProperty.Register("IsFirstMesure", typeof(bool),
           typeof(VueMesure), new PropertyMetadata(false));



        public VueMesure(int numMesure)
        {
            InitializeComponent();
            firstMesure(isFirstMesure);
            numeroMesure = numMesure;
            IsClassicNotationEnabled = App.option.Affichage.IsNotationClassicEnabled;
            isSelected = false;

        }



        public void firstMesure(bool val)
        {

            if (val)
            {
                DebutMesure.Width = 4;

                panelNote.Children.Clear();
                panelNote.Children.Add(new Gkey()); // Clée de Sol
                panelNote.Children.Add(new NumMesure());// Signature rythmique
            }

        }


        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {
            if (numeroMesure != App.MesureSelected)
            {
                Background = (Brush)FindResource("over");

            }

        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            if (numeroMesure != App.MesureSelected)
            {
                Background = (Brush)FindResource("normal");

            }
        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {

            if (!isSelected)
            {
                // on déselectionne la mesure précédement selectionnée
                if (App.MesureSelected != -1)
                {
                    ((VueMesure)App.Pages[App.ModeleurEnCours].VuePiste.Children[App.MesureSelected]).Background
                         = (Brush)FindResource("normal");
                    ((VueMesure)App.Pages[App.ModeleurEnCours].VuePiste.Children[App.MesureSelected]).isSelected = false;
                }

                Background = (Brush)FindResource("selected");

                App.MesureSelected = numeroMesure;
                ((MainWindow)App.Current.MainWindow).Menu.BtnSupprimerMesure.IsEnabled = true;

                App.MesureSelected = numeroMesure;
                ((MainWindow)App.Current.MainWindow).Menu.GroupNote.IsEnabled = true;
                ((MainWindow)App.Current.MainWindow).Menu.GroupSilence.IsEnabled = true;
                
                isSelected = true;
                ((MainWindow)App.Current.MainWindow).Menu.Note.IsSelected = true;
            }
            else
            {
                // on déselectionne la mesure précédement selectionnée
                if (App.MesureSelected != -1)
                {
                    ((VueMesure)App.Pages[App.ModeleurEnCours].VuePiste.Children[App.MesureSelected]).Background
                         = (Brush)FindResource("normal");
                    ((VueMesure)App.Pages[App.ModeleurEnCours].VuePiste.Children[App.MesureSelected]).isSelected = false;
                }

                Background = (Brush)FindResource("normal");
                App.MesureSelected = -1;
                /* On désactive les menus des rubbans concernés*/
                ((MainWindow)App.Current.MainWindow).Menu.BtnSupprimerMesure.IsEnabled = false;


                if (App.NoteSelected == -1)
                {
                    ((MainWindow)App.Current.MainWindow).Menu.GroupNote.IsEnabled = false;
                    ((MainWindow)App.Current.MainWindow).Menu.GroupSilence.IsEnabled = false;
                }
                isSelected = false;
                ((MainWindow)App.Current.MainWindow).Menu.Piste.IsSelected = true;
            }
        }

        public void AjouterNoteClassique(Note n)
        {
            int positionNote = 0;
            bool TigeBas = false;


            switch (n.octave)
            {
                default:
                    switch (n.note)
                    {
                        case Note.NoteEn.C: positionNote = 22; break;
                        case Note.NoteEn.D: positionNote = 14; break;
                        case Note.NoteEn.E: positionNote = 6; break;
                        case Note.NoteEn.F: positionNote = 0; break;
                        case Note.NoteEn.G: positionNote = -7; break;
                        case Note.NoteEn.A: positionNote = -14; break;
                        case Note.NoteEn.B: positionNote = -21; break;
                    }
                    ; break;
                case 4: switch (n.note)
                    {
                      

                        case Note.NoteEn.C: positionNote = 65; break;
                        case Note.NoteEn.D: positionNote = 61; break;
                        case Note.NoteEn.E: positionNote = 55; break;
                        case Note.NoteEn.F: positionNote = 48; break;
                        case Note.NoteEn.G: positionNote = 41; break;
                        case Note.NoteEn.A: positionNote = 35; break;
                        case Note.NoteEn.B: positionNote = 27; break;
                    }
                    ; break;




                case 6: switch (n.note)
                    {
                        case Note.NoteEn.C: positionNote = -28; break;
                        case Note.NoteEn.D: positionNote = -35; break;
                        case Note.NoteEn.E: positionNote = -42; break;
                        case Note.NoteEn.F: positionNote = -49; break;
                        case Note.NoteEn.G: positionNote = -56; break;
                        case Note.NoteEn.A: positionNote = -63; break;
                        case Note.NoteEn.B: positionNote = -70; break;

                    }
                    ;TigeBas= true; break;

                case 7: switch (n.note)
                    {
                        case Note.NoteEn.C: positionNote = -77; break;
                        case Note.NoteEn.D: positionNote = -84; break;
                        case Note.NoteEn.E: positionNote = -91; break;
                        case Note.NoteEn.F: positionNote = -98; break;
                        case Note.NoteEn.G: positionNote = -105; break;
                        case Note.NoteEn.A: positionNote = -112; break;
                        case Note.NoteEn.B: positionNote = -119; break;
                    }
                    ;TigeBas= true;  break;


                case 8: switch (n.note)
                    {

                        case Note.NoteEn.C: positionNote = -126; break;
                        case Note.NoteEn.D: positionNote = -133; break;
                        case Note.NoteEn.E: positionNote = -140; break;
                        case Note.NoteEn.F: positionNote = -147; break;

                    }
                    ;TigeBas= true; break;
            }

            VueNote vn = new VueNote(n);

            /* On gére le sens de la tige de manière automatique */
            vn.SensTige(TigeBas);
            vn.Margin = new Thickness(0, positionNote, 0, 0);
            panelNote.Children.Add(vn);
          
        }


        public void AjouterNoteTab(Note n, bool auto)
        {

            int positionNote=0;

            if (auto)
            {
                App.Partitions[App.PartitionEnCours].pistes[App.PisteEnCours].tablature.UpdateNotePossible();
                App.Partitions[App.PartitionEnCours].pistes[App.PisteEnCours].trouverNoteTablature(
                    n, App.Partitions[App.PartitionEnCours].pistes[App.PisteEnCours].tablature);
            }

       
            switch (n.tablatureInfos.numCorde)
            {
                case 0: positionNote = 39; break;
                case 1: positionNote = 25; break;
                case 2: positionNote = 11; break;
                case 3: positionNote = -3; break;
                case 4: positionNote = -17; break;
                case 5: positionNote = -31; break;

            }

            VueNoteTab vn = new VueNoteTab(n, n.tablatureInfos.numCase);
            vn.Margin = new Thickness(0, positionNote, 0, 0);
            panelNoteTab.Children.Add(vn);
        }



        public void AjouterSilence(Note n)
        {

           // int positionNote = 0;

            VueSilence vs = new VueSilence(n);
           //vs.Margin = new Thickness(0, positionNote, 0, 0);
            panelNote.Children.Add(vs);

            VueSilence vs2 = new VueSilence(n);
            vs2.SetAllInvisible();
           // vn.Margin = new Thickness(0, positionNote, 0, 0);
            panelNoteTab.Children.Add(vs2);
        }

    }
}
